package service

import (
	"fmt"
	"strings"

	"github.com/go-xorm/xorm"

	"read.com/wxmp/dao"
)

func GetUserId(db *xorm.Session, unionId string) string {
	if unionId == "" {
		return ""
	}

	if db == nil {
		db = dao.GetSession()
		defer db.Close()
	}

	// 书城用户查询
	userWxmp := &dao.UserWxmp{
		UnionId: unionId,
	}
	if has, err := db.Cols("user_id").Get(userWxmp); err != nil {
		fmt.Println("查询书城用户错误:", err)
		return ""
	} else if !has {
		return ""
	}
	return userWxmp.UserId
}

func GetBookReadCount(db *xorm.Session, userId string) int {
	record := &dao.RecReading{
		UserId: userId,
	}
	if has, err := db.Cols("book_id").Get(record); err != nil {
		fmt.Println("查询阅读书籍错误:", err)
		return 0
	} else if !has {
		return 0
	}
	books := strings.Split(record.BookId, ",")
	return len(books)
}
